package com.gxa.autoparts.portal.service.impl;

import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.*;
import com.gxa.autoparts.core.service.impl.BaseServiceImpl;
import com.gxa.autoparts.core.util.JsonUtil;
import com.gxa.autoparts.core.util.SnowFlakeUtil;
import com.gxa.autoparts.core.util.ThreadLocalUtil;
import com.gxa.autoparts.portal.mapper.MyOrderMapper;
import com.gxa.autoparts.portal.service.AfterSaleService;
import com.gxa.autoparts.portal.service.CheckMyCouponService;
import com.gxa.autoparts.portal.service.MyOrderService;
import com.gxa.autoparts.portal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("myOrderService")
public class MyOrderServiceImpl extends BaseServiceImpl<Order> implements MyOrderService {
    @Autowired
    MyOrderMapper myOrderMapper;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Autowired
    UserService userService;
    @Autowired
    CheckMyCouponService checkMyCouponService;
    @Autowired
    AfterSaleService afterSaleService;

    private static final String COMMENT = "COMMENT:";
    private static final String COMMENTS = "COMMENTS:";
    @Override
    public List<Order> checkMyOrder(Long userId) {
        List<Order> allMyOrder = myOrderMapper.findAllMyOrder(userId);
        for (Order order : allMyOrder) {
            Address orderAddress = myOrderMapper.findOrderAddress(userId, order.getId());
            order.setAddress(orderAddress);
            List<GoodsList> orderGoodsList = myOrderMapper.findOrderGoodsList(userId, order.getId());
            order.setGoodsList(orderGoodsList);
        }
        return allMyOrder;
    }

    @Override
    public void addComment(Long userId,Long goodsId,String context,Integer star) {
        Comment comment = new Comment();
        User user = userService.selectById(userId);
        comment.setContext(context);
        comment.setImg(user.getImg());
        comment.setStart(star);
        comment.setUserId(userId);
        comment.setUsername(user.getNickName());
        comment.setGoodsId(goodsId);
        ListOperations listOperations = redisTemplate.opsForList();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        comment.setId(SnowFlakeUtil.nextId());
        comment.setCreateTime(new Date());
        valueOperations.set(COMMENT+comment.getId(),JsonUtil.ObjectToString(comment));
        listOperations.leftPush(COMMENTS+comment.getGoodsId(),comment.getId());
    }

    @Override
    public Comment checkMyComment(Long userId, Long goodsId) throws BusinessException {
        ListOperations listOperations = redisTemplate.opsForList();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List range = listOperations.range(COMMENTS + goodsId, 0, -1);
        for (Object o : range) {
            String s = valueOperations.get(COMMENT + o).toString();
            Comment comment = JsonUtil.readToObject(s, Comment.class);
            if (comment.getUserId() == userId){
                return comment;
            }
        }
        throw new BusinessException(ExceptionCode.NO_COMMENT);
    }

    @Override
    public Map<String, Object> count(Long userId) {
        Map<String, Object> map = new HashMap<>();
        User user = userService.selectById(userId);
        map.put("user",user);
        List<Coupon> coupons = checkMyCouponService.unusedCouponList(userId);
        List<Coupon> coupons1 = checkMyCouponService.expiredCouponList(userId);
        List<Coupon> coupons2 = checkMyCouponService.usedCouponList(userId);
        int couponNum = coupons.size()+coupons1.size()+coupons2.size();
        map.put("couponNum",couponNum);
        List<Order> orders = checkMyOrder(userId);
        int orderState1 = 0;
        int orderState2 = 0;
        int orderState3 = 0;
        int orderState4 = 0;
        int orderState5 = 0;
        for (Order order : orders) {
            if (order.getState() ==1){
                orderState1++;
            }
            if (order.getState() ==2){
                orderState2++;
            }
            if (order.getState() ==3){
                orderState3++;
            }
            if (order.getState() ==4){
                orderState4++;
            }
            if (order.getState() ==5){
                orderState5++;
            }

        }
        map.put("orderState1",orderState1);
        map.put("orderState2",orderState2);
        map.put("orderState3",orderState3);
        map.put("orderState4",orderState4);
        map.put("orderState5",orderState5);
        List<AfterSale> afterSales = afterSaleService.checkMyAfterSale(userId);
        map.put("afterSaleNum",afterSales.size());
        return map;
    }

    @Override
    public Double getMoney(Long orderId) {
        return myOrderMapper.getMoney(orderId);
    }

    @Override
    public void overOrder(Long orderId, Long logisticsNumber) {
        myOrderMapper.overOrder(orderId,logisticsNumber);
    }

    @Override
    public Map<String, Object> findOrderIdBySerialnNumber(Long serialnNumber) {
        return myOrderMapper.findOrderIdBySerialnNumberAndPrice(serialnNumber);
    }


}
